<template>
  <div class="home">
    <van-nav-bar
      title="我的主页"
      left-text="返回"
      right-text="test"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
      :fixed="navBarFixed"
    />
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell ref="itemBox"
        v-for="item in list"
        :key="item"
        :title="'我是第' + item + '条'"
        @click.native="handleClick"
      />
    </van-list>
  </div>
</template>

<script>
export default {
  name: 'home',
   data() {
    return {
      list: [],
      loading: false,
      finished: false,
      navBarFixed: true
    };
  },
  activated(){
    if(!this.$route.meta.isBack){
        //操作，从其他页面进入时，可以操作恢复默认状态、请求接口。。。
    }else{
        //详情页返回操作，比如页面位置
    }
},

  beforeRouteEnter(to,from,next){
    if(from.name == '/detail'){
        to.meta.isBack = true;
    }else{
        to.meta.isBack = false;
    }
    next();
  },
  computed: {
    title() {
      return this.$route.name;
    }
  },

  methods: {
    onClickLeft() {
      Toast('返回');
    },
    onClickRight() {
      this.$router.push('/test')
    },
    handleClick () {
      this.$router.push('/detail');
    },
    onLoad() {
      // 异步更新数据
      setTimeout(() => {
        for (let i = 0; i < 10; i++) {
          this.list.push(this.list.length + 1);
        }
        // 加载状态结束
        this.loading = false;

        // 数据全部加载完成
        if (this.list.length >= 400) {
          this.finished = true;
        }
      }, 500);
    }
  }
}
</script>

<style lang="less">
  .home /deep/ .van-list {
    padding-top: 50px;
  }
  .home /deep/ .van-cell {
    line-height: 50px;
    border-bottom: 1px solid #f7f7f7;
  }
</style>
